package GFAlgorithm;

public class GFAlgorithmTest {

    private static final GFAlgorithm gfAlgorithm = new GFAlgorithm();

    public static void main(String[] args) {
        GFAlgorithmTest gfAlgorithmTest = new GFAlgorithmTest();
        gfAlgorithmTest.GF4OpAddTest();
        gfAlgorithmTest.GF4OpMinusTest();
        gfAlgorithmTest.GF4OpMultiTest();
        gfAlgorithmTest.GF4OpDivideTest();
        gfAlgorithmTest.GFEuclidTest();
        gfAlgorithmTest.GFInverseModTest();
    }

    void GF4OpAddTest() {
        int[][] testDataAdd = new int[3][3];
        testDataAdd[0][0] = 0x89;
        testDataAdd[0][1] = 0x4d;
        testDataAdd[0][2] = 0xc4;

        testDataAdd[1][0] = 0xaf;
        testDataAdd[1][1] = 0x3b;
        testDataAdd[1][2] = 0x94;

        testDataAdd[2][0] = 0x35;
        testDataAdd[2][1] = 0xc6;
        testDataAdd[2][2] = 0xf3;

        boolean flag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            if (testDataAdd[i][2] != gfAlgorithm.addOrMinus(testDataAdd[i][0], testDataAdd[i][1])) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("GF4OpAddTest：加法测试通过");
        } else {
            System.out.println("GF4OpAddTest：加法测试不通过");
        }
    }

    void GF4OpMinusTest() {
        int[][] testDataAdd = new int[3][3];
        testDataAdd[0][0] = 0x89;
        testDataAdd[0][1] = 0x4d;
        testDataAdd[0][2] = 0xc4;

        testDataAdd[1][0] = 0x9f;
        testDataAdd[1][1] = 0x3b;
        testDataAdd[1][2] = 0xa4;

        testDataAdd[2][0] = 0x35;
        testDataAdd[2][1] = 0xc6;
        testDataAdd[2][2] = 0xf3;

        boolean flag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            if (testDataAdd[i][2] != gfAlgorithm.addOrMinus(testDataAdd[i][0], testDataAdd[i][1])) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("GF4OpMinusTest：减法测试通过");
        } else {
            System.out.println("GF4OpMinusTest：减法测试不通过");
        }
    }

    void GF4OpMultiTest() {
        int[][] testDataAdd = new int[3][3];
        testDataAdd[0][0] = 0xce;
        testDataAdd[0][1] = 0xf1;
        testDataAdd[0][2] = 0xef;

        testDataAdd[1][0] = 0x70;
        testDataAdd[1][1] = 0x99;
        testDataAdd[1][2] = 0xa2;

        testDataAdd[2][0] = 0x00;
        testDataAdd[2][1] = 0xa4;
        testDataAdd[2][2] = 0x00;

        boolean flag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            if (testDataAdd[i][2] != gfAlgorithm.multiply(testDataAdd[i][0], testDataAdd[i][1], 283)) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("GF4OpMultiTest：乘法测试通过");
        } else {
            System.out.println("GF4OpMultiTest：乘法测试不通过");
        }
    }

    void GF4OpDivideTest() {
        int[][] testDataAdd = new int[3][3];
        testDataAdd[0][0] = 0xde;
        testDataAdd[0][1] = 0xc6;
        testDataAdd[0][2] = 0x01;

        testDataAdd[1][0] = 0x8c;
        testDataAdd[1][1] = 0x0a;
        testDataAdd[1][2] = 0x14;

        testDataAdd[2][0] = 0x3e;
        testDataAdd[2][1] = 0xa4;
        testDataAdd[2][2] = 0x00;

        boolean flag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            if (testDataAdd[i][2] != gfAlgorithm.divide(testDataAdd[i][0], testDataAdd[i][1])) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("GF4OpDivideTest：除法测试通过");
        } else {
            System.out.println("GF4OpDivideTest：除法测试不通过");
        }
    }

    void GFEuclidTest() {
        int[][] testDataAdd = new int[4][5];
        testDataAdd[0][0] = 0x75;
        testDataAdd[0][1] = 0x35;
        testDataAdd[0][2] = 0x19;
        testDataAdd[0][3] = 0x3c;
        testDataAdd[0][4] = 0x01;

        testDataAdd[1][0] = 0xac;
        testDataAdd[1][1] = 0x59;
        testDataAdd[1][2] = 0x07;
        testDataAdd[1][3] = 0x0f;
        testDataAdd[1][4] = 0x03;

        testDataAdd[2][0] = 0xf8;
        testDataAdd[2][1] = 0x2e;
        testDataAdd[2][2] = 0x04;
        testDataAdd[2][3] = 0x1b;
        testDataAdd[2][4] = 0x02;

        testDataAdd[3][0] = 0x48;
        testDataAdd[3][1] = 0x99;
        testDataAdd[3][2] = 0x02;
        testDataAdd[3][3] = 0x01;
        testDataAdd[3][4] = 0x09;

        boolean euclidFlag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            if (testDataAdd[i][4] != gfAlgorithm.euclid(testDataAdd[i][0], testDataAdd[i][1])) {
                euclidFlag = false;
            }
        }
        if (euclidFlag) {
            System.out.println("GFEuclidTest：欧几里得算法测试通过");
        } else {
            System.out.println("GFEuclidTest：欧几里得算法测试不通过");
        }

        for (int i = 0; i < testDataAdd.length; i++) {
            if (gfAlgorithm.addOrMinus(gfAlgorithm.multiply(testDataAdd[i][0], testDataAdd[i][2], 283),
                    gfAlgorithm.multiply(testDataAdd[i][1], testDataAdd[i][3], 283)) != testDataAdd[i][4]) {
                System.out.println("测试用例有错误!");
            }
        }

        boolean exEuclidFlag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            int[] exEuclid = gfAlgorithm.exEuclid(testDataAdd[i][0], testDataAdd[i][1]);
            if (testDataAdd[i][4] != exEuclid[0]
                    || testDataAdd[i][2] != exEuclid[1]
                    || testDataAdd[i][3] != exEuclid[2]) {
                exEuclidFlag = false;
            }
        }
        if (exEuclidFlag) {
            System.out.println("GFEuclidTest：扩展欧几里得算法测试通过");
        } else {
            System.out.println("GFEuclidTest：扩展欧几里得算法测试不通过");
        }
    }

    void GFInverseModTest() {
        int[][] testDataAdd = new int[4][2];
        testDataAdd[0][0] = 0x8c;
        testDataAdd[0][1] = 0xf7;

        testDataAdd[1][0] = 0xbe;
        testDataAdd[1][1] = 0x86;

        testDataAdd[2][0] = 0x01;
        testDataAdd[2][1] = 0x01;

        testDataAdd[3][0] = 0x2d;
        testDataAdd[3][1] = 0x44;

        boolean flag = true;
        for (int i = 0; i < testDataAdd.length; i++) {
            // m是一个不可约多项式
            if (testDataAdd[i][1] != gfAlgorithm.inverseMod(testDataAdd[i][0], 283)) {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("GFInverseModTest：模逆测试通过");
        } else {
            System.out.println("GFInverseModTest：模逆测试不通过");
        }
    }

}
